'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 技能
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var taskService = require('./taskService');
var fightPowerService = require('./fightPowerService.js');
var skillConf = require('../config/skillConf');
var dataApi = require('../libs/dataApi');

exports.upgradeSkill = function (player_manager, skill_type) {
    var skill_model = player_manager.skill;
    var skill_item = skill_model.getSkill(skill_type);
    if (!skill_item) {
        return false;
    }
    skill_item.upgrade();
    taskService.addHeroSkillLv(player_manager, skill_item.getLv());
    return true;
};

/**
 * 更新技能战力
 * @return {[type]}             [description]
 */
exports.updateSkillPower = function (player_manager, update_single) {
    var skill_model = player_manager.skill;
    var sel_skill_type = {};
    _.each(skill_model.select_skill_list, function (skill_type) {
        sel_skill_type[skill_type] = 1;
    });

    var skill_power = 0;
    var select_skill_power = 0;
    _.each(skill_model.skill_list, function (skill_item) {
        var skill_conf = skillConf.getSkillLvConf(skill_item.skill_type, skill_item.lv);
        skill_power += skill_conf.fp;
        if (!!sel_skill_type[skill_item.skill_type]) {
            select_skill_power += skill_conf.fp;
        }
    });
    skill_model.skill_power = skill_power;
    skill_model.select_skill_power = select_skill_power;

    if (!update_single) {
        // 刷新总战力
        fightPowerService.updByIdMainPower(player_manager.getRoleId(), player_manager, function (err) {
            if (!err) {
                player_manager.save();
            }
        });
    }
};

var skill_map = {};

skill_map[1001] = 8001;
skill_map[1002] = 8002;
skill_map[1003] = 8003;
skill_map[1004] = 8004;
skill_map[1005] = 8101;
skill_map[1006] = 8005;
skill_map[1007] = 8006;
skill_map[1008] = 8007;
skill_map[2001] = 8001;
skill_map[2002] = 8002;
skill_map[2003] = 8003;
skill_map[2004] = 8004;
skill_map[2005] = 8201;
skill_map[2006] = 8005;
skill_map[2007] = 8006;
skill_map[2008] = 8007;

/**
 * 更新技能
 */
exports.migrateSkill = function (player_manager) {
    var skill_model = player_manager.skill;
    var is_new_skill = false;
    _.forEach(skill_model.skill_list, function (item) {
        if (item.skill_type === 8001) {
            is_new_skill = true;
            return false;
        }

        if (item.skill_type === 10002) {
            is_new_skill = false;
            return false;
        }

        return true;
    });

    if (is_new_skill) {
        _.forEach(skill_model.skill_list, function (item) {
            if (item.skill_type <= 2008) {
                is_new_skill = false;
                return false;
            }
            return true;
        });
    }

    if (is_new_skill) {
        return;
    }

    _.forEach(skill_model.skill_list, function (item) {
        var new_type = skill_map[item.skill_type];
        if (!!new_type) {
            item.skill_type = new_type;
        }
        var skill_conf = dataApi.hero_skill_lvl.findById(item.skill_type);
        if (!!skill_conf) {
            if (item.lv > skill_conf.length) {
                item.lv = skill_conf.length;
            }
        }
    });

    var select_skill_list = [];
    _.forEach(skill_model.select_skill_list, function (old_skill_type) {
        var new_skill_type = skill_map[old_skill_type];
        if (!!new_skill_type) {
            select_skill_list.push(new_skill_type);
        } else {
            select_skill_list.push(old_skill_type);
        }
    });
    skill_model.setSelectSkillList(select_skill_list);
};
